import sys, exceptions

class State:
    def __init__ (self, actCount = 0, lastAct = 0, switches = 0, actTime = 0):
#    def __init__ (self, act = False, lastAct = 0, actCount = 0, switches = 0, actTime = 0):        
        self.lastActiveTime = lastAct
        self.totalActiveTime = actTime
        self.activationCount = actCount
        self.switchCount = switches

    def reset(self, utcTime):
        self.totalActiveTime = 0
        self.switchCount = 0
        self.lastActiveTime = utcTime

    def activate (self, utcTime):
        self.activationCount += 1

        if self.activationCount == 1:
            self.lastActiveTime = utcTime         
            return True
        
        return False
    
    def deactivate (self, utcTime):
        self.activationCount -= 1
        if self.activationCount < 0:
            raise exceptions.ValueError("State.activationCount dropped below 0")
        
        if self.activationCount == 0:
            self.totalActiveTime += (utcTime - self.lastActiveTime)
            return True

        return False

    def isActive(self):
        return self.activationCount > 0

    def activeTime(self, utcTime):
        totalTime = self.totalActiveTime
        if self.isActive():
            totalTime += (utcTime - self.lastActiveTime)
        return totalTime
    
    def addSwitch(self):
        self.switchCount += 1